home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / Kant Pro source Folder / Kant Pro 1.1 ƒ / Shell ƒ / graphics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-16  |  7.6 KB  |  292 lines  |  [TEXT/MMCC]

  1. #include "graphics.h"
  2. #include "graphics dispatch.h"
  3. #include "window layer.h"
  4. #include <QDOffscreen.h>
  5.  
  6. extern    Boolean            gHasColorQD;    /* see environment.c */
  7.  
  8. static    void GetMainScreenBounds(Rect *screenRect);
  9. static    short GetBiggestDeviceDepth(WindowPtr theWindow);
  10. static    short GetWindowRealDepth(WindowPtr theWindow);
  11.  
  12. OSErr OpenTheIndWindow(short index)
  13. {
  14.     WindowPtr        theWindow;
  15.     Point            topLeft;
  16.     ExtendedWindowPtr    storage;
  17.     Rect            screenRect;
  18.     Rect            boundsRect;
  19.     short            windowType;
  20.     
  21.     if (!IndWindowExistsQQ(index))
  22.     {
  23.         storage=(ExtendedWindowPtr)NewPtr(sizeof(ExtendedWindowRec));
  24.         if (storage==0L)
  25.             return MemError();
  26.         
  27.         theWindow=(WindowPtr)storage;
  28.         SetupWindowDispatch(index, theWindow);
  29.         windowType=GetWindowType(theWindow);
  30.         
  31.         if (WindowAutoCenterQQ(theWindow))
  32.         {
  33.             GetMainScreenBounds(&screenRect);
  34.             topLeft.h=screenRect.left+
  35.                 (((screenRect.right-screenRect.left)-GetWindowWidth(theWindow))/2);
  36.             topLeft.v=screenRect.top+
  37.                 (((screenRect.bottom-screenRect.top)-GetWindowHeight(theWindow))/2);
  38.             SetWindowTopLeft(theWindow, topLeft);
  39.             if ((windowType==noGrowDocProc) || (windowType==documentProc) ||
  40.                 (windowType==movableDBoxProc) || (windowType==zoomDocProc) ||
  41.                 (windowType==zoomNoGrow) || (windowType==rDocProc))
  42.             {
  43.                 topLeft.v+=9;
  44.             }
  45.         }
  46.         else
  47.         {
  48.             topLeft=GetWindowTopLeft(theWindow);
  49.         }
  50.         
  51.         if (topLeft.v<GetMBarHeight()+1)
  52.             topLeft.v=GetMBarHeight()+1;
  53.         
  54.         SetRect(&boundsRect, topLeft.h, topLeft.v, topLeft.h+GetWindowWidth(theWindow),
  55.             topLeft.v+GetWindowHeight(theWindow));
  56.         SetWindowBounds(theWindow, boundsRect);
  57.         
  58.         if (gHasColorQD)
  59.         {
  60.             if (WindowIsFloatQQ(theWindow))
  61.             {
  62.                 theWindow=MyNewFloatCWindow(storage, &(GetWindowBounds(theWindow)),
  63.                     GetWindowTitle(theWindow), FALSE, windowType, (WindowPtr)-1L,
  64.                     WindowHasCloseBoxQQ(theWindow), 0L);
  65.             }
  66.             else
  67.             {
  68.                 theWindow=MyNewCWindow(storage, &(GetWindowBounds(theWindow)),
  69.                     GetWindowTitle(theWindow), FALSE, windowType, (WindowPtr)-1L,
  70.                     WindowHasCloseBoxQQ(theWindow), 0L);
  71.             }
  72.         }
  73.         else
  74.         {
  75.             if (WindowIsFloatQQ(theWindow))
  76.             {
  77.                 theWindow=MyNewFloatWindow(storage, &(GetWindowBounds(theWindow)),
  78.                     GetWindowTitle(theWindow), FALSE, windowType, (WindowPtr)-1L,
  79.                     WindowHasCloseBoxQQ(theWindow), 0L);
  80.             }
  81.             else
  82.             {
  83.                 theWindow=MyNewWindow(storage, &(GetWindowBounds(theWindow)),
  84.                     GetWindowTitle(theWindow), FALSE, windowType, (WindowPtr)-1L,
  85.                     WindowHasCloseBoxQQ(theWindow), 0L);
  86.             }
  87.         }
  88.     }
  89.     
  90.     if (IndWindowExistsQQ(index))
  91.     {
  92.         theWindow=GetIndWindowPtr(index);
  93.         MySelectWindow(theWindow);        /* immediately select this new window */
  94.         SetPort(theWindow);                /* important! for TE info to stick*/
  95.         InvalRect(&(theWindow->portRect));
  96.         /* call window's dispatch routine to alert it that it's open now */
  97.         OpenWindowDispatch(index);
  98. //        ActivateWindowDispatch(index);
  99.     }
  100.     else return -1;
  101.     
  102.     return noErr;
  103. }
  104.  
  105. OSErr UpdateTheWindow(WindowPtr theWindow)
  106. {
  107.     short            index;
  108.     long            offRowBytes, sizeOfOff;
  109.     PixMapHandle    thePixMapHandle;
  110.     GWorldPtr        currentGWorld;
  111.     GDHandle        currentGDHandle;
  112.     Rect            boundsRect;
  113.     GWorldPtr        theGWorld;
  114.     Ptr                bwBitMap;
  115.     GrafPort        bwGrafPort;
  116.     GrafPtr            bwGrafPtr;
  117.     OSErr            memError;
  118.     TEHandle        hTE;
  119.     short            realWindowDepth, maxWindowDepth;
  120.     
  121.     index=GetWindowIndex(theWindow);
  122.     realWindowDepth=GetWindowRealDepth(theWindow);
  123.     maxWindowDepth=GetWindowMaxDepth(theWindow);
  124.     boundsRect=theWindow->portRect;
  125.     OffsetRect(&boundsRect, -boundsRect.left, -boundsRect.top);
  126.  
  127.     if (gHasColorQD)    /* w/o Color Quickdraw, GWorlds may not be supported */
  128.     {
  129.         /* try to create new graphics world; display error if unsuccessful */
  130.         if (NewGWorld(&theGWorld, (realWindowDepth>=maxWindowDepth) ? maxWindowDepth : 0,
  131.                 &boundsRect, 0L, 0L, 0)!=0)
  132.         {
  133.             return MemError();
  134.         }
  135.         
  136.         NoPurgePixels(GetGWorldPixMap(theGWorld));    /* never purge our pixmap! */
  137.         
  138.         GetGWorld(¤tGWorld, ¤tGDHandle);    /* get current settings */
  139.         LockPixels(thePixMapHandle=GetGWorldPixMap(theGWorld));    /* important!  copybits may move mem */
  140.         /* update offscreen graphics world, compensating for change in pixel depth */
  141.         UpdateGWorld(&theGWorld, (realWindowDepth>=maxWindowDepth) ? maxWindowDepth : 0,
  142.                 &boundsRect, 0L, 0L, 0);
  143.         SetGWorld(theGWorld, 0L);                /* set to our offscreen gworld */
  144.         
  145.         if ((**(**(GetGWorldDevice(theGWorld))).gdPMap).pixelSize!=GetWindowDepth(theWindow))
  146.             ChangeDepthDispatch(index, GetWindowDepth(theWindow));
  147.     }
  148.     else    /* deal with (guaranteed) B/W bitmaps manually */
  149.     {
  150.         bwGrafPtr=&bwGrafPort;
  151.         OpenPort(bwGrafPtr);
  152.         offRowBytes=(((boundsRect.right-boundsRect.left)+15)>>4)<<1;
  153.         sizeOfOff=(long)(boundsRect.bottom-boundsRect.top)*offRowBytes;
  154.         bwBitMap=NewPtr(sizeOfOff);
  155.         if (bwBitMap==0L)
  156.         {
  157.             memError=MemError();
  158.             ClosePort(bwGrafPtr);
  159.             return memError;
  160.         }
  161.         
  162.         bwGrafPort.portBits.baseAddr=bwBitMap;
  163.         bwGrafPort.portBits.rowBytes=offRowBytes;
  164.         bwGrafPort.portBits.bounds=bwGrafPort.portRect=boundsRect;
  165.         
  166.         SetPort(bwGrafPtr);
  167.     }    
  168.     
  169.     SetWindowDepth(theWindow, (realWindowDepth>maxWindowDepth) ? maxWindowDepth : realWindowDepth);
  170.     if (DrawWindowDispatch(index, GetWindowDepth(theWindow))==kFailure)
  171.     {
  172.         EraseRect(&(theWindow->portRect));
  173.     }
  174.     
  175.     if ((hTE=GetWindowTE(theWindow))!=0L)
  176.     {
  177.         (**hTE).inPort=gHasColorQD ? (GrafPtr)theGWorld : bwGrafPtr;
  178.         EraseRect(&((**hTE).viewRect));
  179.         TEUpdate(&(theWindow->portRect), hTE);
  180.         (**hTE).inPort=theWindow;
  181.     }
  182.     
  183.     if (gHasColorQD)
  184.         SetGWorld(currentGWorld, currentGDHandle);
  185.     
  186.     SetPort(theWindow);
  187.     
  188.     if (CopybitsDispatch(index, gHasColorQD ? (WindowPtr)theGWorld : (WindowPtr)bwGrafPtr)==kFailure)
  189.     {
  190.         CopyBits(gHasColorQD ?
  191.                     &(((WindowPtr)theGWorld)->portBits) :
  192.                     &(((WindowPtr)bwGrafPtr)->portBits),
  193.                 &(theWindow->portBits),
  194.                 &boundsRect,
  195.                 &boundsRect,
  196.                 0,
  197.                 0L);
  198.         if ((GetWindowVScrollBar(theWindow)!=0L) || (GetWindowHScrollBar(theWindow)!=0L))
  199.             UpdateControls(theWindow, theWindow->visRgn);
  200.     }
  201.     
  202.     ValidRect(&(theWindow->portRect));
  203.     
  204.     if (gHasColorQD)
  205.     {
  206.         UnlockPixels(thePixMapHandle);
  207.         DisposeGWorld(theGWorld);
  208.     }
  209.     else
  210.     {
  211.         ClosePort(bwGrafPtr);
  212.         DisposePtr(bwBitMap);
  213.     }
  214.     
  215.     CompactMem(maxSize);
  216.     
  217.     return noErr;
  218. }
  219.  
  220. Boolean CloseTheWindow(WindowPtr theWindow)
  221. {
  222.     short            index;
  223.     
  224.     index=GetWindowIndex(theWindow);
  225.     
  226.     if (CloseWindowDispatch(index)==kCancel)
  227.         return FALSE;
  228.     
  229.     DisposeWindowDispatch(index);
  230.     
  231.     MyDisposeWindow(theWindow);
  232.     
  233.     return TRUE;    /* successful close */
  234. }
  235.  
  236. /* -------------------------------------------- */
  237. /* the rest of these are internal to graphics.c */
  238.  
  239. static    void GetMainScreenBounds(Rect *screenRect)
  240. {
  241.     *screenRect = qd.screenBits.bounds;        /* low-mem global */
  242.     (*screenRect).top += GetMBarHeight();    /* don't include menu bar */
  243. }
  244.  
  245. static    short GetBiggestDeviceDepth(WindowPtr theWindow)
  246. {
  247.     Rect            tempRect;
  248.     long            biggestSize;
  249.     long            tempSize;
  250.     GDHandle        thisHandle, gBiggestDevice;
  251.     
  252.     if (!gHasColorQD)
  253.         return 1;
  254.     
  255.     if (!theWindow)
  256.         return (**(**GetMainDevice()).gdPMap).pixelSize;
  257.     
  258.     thisHandle = GetDeviceList();
  259.     gBiggestDevice = 0L;
  260.     biggestSize = 0L;
  261.     
  262.     while (thisHandle)
  263.     {
  264.         if (TestDeviceAttribute(thisHandle, screenDevice) &&
  265.             TestDeviceAttribute(thisHandle, screenActive))
  266.         {
  267.             if (SectRect(&(theWindow->portRect),
  268.                     &((**thisHandle).gdRect), &tempRect))
  269.             {
  270.                 if (biggestSize < (tempSize = ((long)(tempRect.bottom - tempRect.top))*
  271.                     ((long)(tempRect.right - tempRect.left))))
  272.                 {
  273.                     biggestSize = tempSize;
  274.                     gBiggestDevice = thisHandle;
  275.                 }
  276.             }
  277.         }
  278.         thisHandle = GetNextDevice(thisHandle);
  279.     }
  280.     
  281.     return (gBiggestDevice) ? (**(**gBiggestDevice).gdPMap).pixelSize : 1;
  282. }
  283.  
  284. static    short GetWindowRealDepth(WindowPtr theWindow)
  285. {
  286.     return (gHasColorQD) ?
  287.                 ((theWindow) ?
  288.                     GetBiggestDeviceDepth(theWindow) :
  289.                     (**(**GetMainDevice()).gdPMap).pixelSize) :
  290.             1;
  291. }
  292.